/*
 * Design an algorithm and write code to remove the duplicate characters in a string
 * without using any additional buffer.
 * NOTE: One or two additional variables are fine. An extra copy of the array is not.
 * FOLLOW UP
 * Write the test cases for this method.
 */

/*
 * since it's a c style string, I'll use an extra array to contain the string
 */
public class Chapter1_3 {

//	public static void main(String []args)
//	{
//		Chapter1_3 test = new Chapter1_3();
//		test.removeDupCharacter("aaaabbcddae");
//		test.removeDupCharacter("abcd");
//		test.removeDupCharacter(null);
//		test.removeDupCharacter("aaa");
//	}
	
	public void removeDupCharacter(String str)
	{
		if(str == null) return;
		if(str.length() < 2) return;
		
		char strOfChars[] = str.toCharArray();
		int tail = 1, j = 0;
		for(int i = 0; i < str.length(); i++)
		{
			for(j = 0; j < tail; j++)
			{
				if(strOfChars[i] == strOfChars[j]) break;
			}
			if(j == tail){
				strOfChars[tail] = strOfChars[i];
				tail++;
			}
		}
		
		String newStr = new String(strOfChars, 0, tail);
		System.out.println(newStr);
	}
}
